home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / getopt.c < prev    next >
C/C++ Source or Header  |  1995-05-11  |  2KB  |  109 lines

  1. /* getopt.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: getopt.c,v 4.12 1995/05/11 12:04:43 espie Exp espie $
  6.  * $Log: getopt.c,v $
  7.  * Revision 4.12  1995/05/11  12:04:43  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.11  1995/02/21  17:54:32  espie
  11.  * Internal problem: buggy RCS. Fixed logs.
  12.  *
  13.  * Revision 4.6  1995/02/01  20:41:45  espie
  14.  * Added color.
  15.  *
  16.  * Revision 4.5  1995/02/01  16:39:04  espie
  17.  * Moved includes to defs.h
  18.  *
  19.  * Revision 1.5  1993/12/04  16:12:50  espie
  20.  * New getopt semantics.
  21.  */
  22.  
  23. #include <ctype.h>
  24.  
  25. #include "defs.h"
  26. #include "getoption.h"
  27.  
  28. ID("$Id: getopt.c,v 4.12 1995/05/11 12:04:43 espie Exp espie $")
  29.  
  30. int optind = 1;
  31. char *optarg = 0;
  32. LOCAL not_an_option = 0;
  33.  
  34. LOCAL int parse_option(argv, option)
  35. char *argv[];
  36. struct long_option *option;
  37.     {
  38.     optind++;
  39.     if (option->argn)
  40.         optarg = argv[optind++];
  41.     return option->code;
  42.     }
  43.  
  44. int getlongopt(argc, argv, options)
  45. int argc;
  46. char *argv[];
  47. struct long_option *options;
  48.     {
  49.     if (not_an_option == optind)
  50.         return -1;
  51.     if (optind >= argc)
  52.         return -1;
  53.     if (argv[optind][0] == '-')
  54.         {
  55.         char *match = argv[optind]+1;
  56.         if (strlen(match) == 1)
  57.             {
  58.             if (match[0] == '-')
  59.                 {
  60.                 not_an_option = ++optind;
  61.                 return -1;
  62.                 }
  63.             while(options->fulltext)
  64.                 {
  65.                 if (options->abbrev == match[0])
  66.                     return parse_option(argv, options);
  67.                 else
  68.                     options++;
  69.                 }
  70.             return -1;
  71.             }
  72.         else
  73.             {
  74.             int max_match = 0;
  75.             struct long_option *best = 0;
  76.  
  77.             while (options->fulltext)
  78.                 {
  79.                 int i;
  80.                 for (i = 0; ; i++)
  81.                     {
  82.                     if (options->fulltext[i] == 0 && match[i] == 0)
  83.                         return parse_option(argv, options);
  84.                     if (match[i] == 0)
  85.                         {
  86.                         if (i > max_match)
  87.                             {
  88.                             max_match = i;
  89.                             best = options;
  90.                             }
  91.                         break;
  92.                         }
  93.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  94.                         break;
  95.                     }
  96.                 options++;
  97.                 }
  98.             if (max_match < 3)
  99.                 {
  100.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  101.                 return -1;
  102.                 }
  103.             return parse_option(argv, best);
  104.             }
  105.         }
  106.     else
  107.         return -1;
  108.     }
  109.